package com.bci.pwtz.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.controller.admin.AdminAbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Production;
import com.bci.pwtz.mysql.model.Resource;
import com.bci.pwtz.service.ProductionService;

@Controller
@RequestMapping("/production")
public class ProductionController extends AdminAbstractController{
	@Autowired
private ProductionService service;

	 @RequestMapping(value="/list",method=RequestMethod.POST)
	 @ResponseBody	
	public List<Production> list(){
		 List<Production> list = new ArrayList<Production>();
		 try {
			list= service.getProList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	 @RequestMapping(value="/load",method=RequestMethod.POST)
	 @ResponseBody	
	 public Production load(int id){
		 Production pro =new Production();
		 try {
			pro= service.load(id);
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
		 return pro;
	 }
	 @RequestMapping(value="/add",method=RequestMethod.POST)
	 @ResponseBody	
	 public int add(String data){
		 Map<String, String> map = new HashMap<String, String>();
		 Production pro =new Production();
		 int result =  0; 
		 try {
			//data=java.net.URLDecoder.decode(data,"UTF-8");
         String [] list=decodeURL(data.split("&"));
         for(int i=0;i<list.length;i++){
             String[] array=list[i].split("=");
             if(array.length==1){
                 map.put(array[0], "");
             }else{
                 map.put(array[0], array[1]);
             }
         }
         pro.setName(map.get("name"));
         pro.setImgurl("imgurl");
         pro.setStatus(1);
         result=	service.insert(pro);
		 
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	 }
	 @RequestMapping(value="/update",method=RequestMethod.POST)
	 @ResponseBody	
	 public int update(String  data){
		 Map<String, String> map = new HashMap<String, String>();
		 Production pro =new Production();
		 int result =  0; 
		 try {
			//data=java.net.URLDecoder.decode(data,"UTF-8");
         String [] list=decodeURL(data.split("&"));
         for(int i=0;i<list.length;i++){
             String[] array=list[i].split("=");
             if(array.length==1){
                 map.put(array[0], "");
             }else{
                 map.put(array[0], array[1]);
             }
         }
         pro.setName(map.get("name"));
         pro.setImgurl("imgurl");
         pro.setStatus(1);
         result=	service.update(pro);
		 } catch (PwtzDatabaseException e) {
			 e.printStackTrace();
		 } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 return result;
	 }
	 
	 
}
